import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/utils.dart';
import 'package:geomancy/widgets/common_text.dart';
import 'package:geomancy/widgets/date_picker/index.dart';
import 'package:go_router/go_router.dart';

class DatePickerModal extends StatefulWidget {
  final Widget? header;
  final double? height;
  final String? defaultValue;
  final Function(String value) getValue;
  const DatePickerModal({super.key, this.header, this.height, this.defaultValue, required this.getValue});

  @override
  State<DatePickerModal> createState() => _DatePickerModalState();
}

final DateTime now = DateTime.now();
final int YEAR = now.year;
final int MONTH = now.month;
final int DAY = now.day;
class _DatePickerModalState extends State<DatePickerModal> {
  List<String> months = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
  late String _selectedDate;

  @override
  void initState() {
    super.initState();
    _selectedDate = widget.defaultValue ?? '$YEAR-${MONTH.toString().padLeft(2, '0')}-${DAY.toString().padLeft(2, '0')}';
  }

  cancelHandle() {
    Utils.shake();
    context.pop();
  }

  confirmHandle() {
    Utils.shake();
    context.pop();
    widget.getValue(_selectedDate);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: ColorStyle.themeLightColor,
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(60.w),
        topRight: Radius.circular(60.w),
      ),
      child: SafeArea(
        child: Container(
          width: double.infinity,
          height: widget.height ?? 470.w,
          padding: EdgeInsets.symmetric(horizontal: 40.w),
          child: Column(
            children: [
              if (widget.header != null)
                widget.header!,
              Container(
                height: 74.w,
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(width: 1.w, color: ColorStyle.borderColor)
                    )
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                      onTap: cancelHandle,
                      child: Container(
                        color: Colors.transparent,
                        padding: EdgeInsets.all(23.w),
                        child: Image.asset('assets/icons/icon_picker_close.png', height: 24.w),
                      ),
                    ),
                    GestureDetector(
                      onTap: confirmHandle,
                      child: Container(
                        color: Colors.transparent,
                        padding: EdgeInsets.all(23.w),
                        child: Image.asset('assets/icons/icon_picker_confirm.png', height: 24.w),
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(
                height: 396.w,
                child: Picker(
                    hideHeader: true,
                    itemExtent: 80.w,
                    height: 396.w,
                    backgroundColor: ColorStyle.themeLightColor,
                    adapter: DateTimePickerAdapter(
                      months: months,
                      customColumnType: [0,1,2],
                      yearSuffix: '年',
                      yearEnd: now.year + 50,
                      daySuffix: '日',
                      value: DateTime.parse(_selectedDate)
                    ),
                    textStyle: TextStyle(color: ColorStyle.textColor, fontSize: 30.sp),
                    selectedTextStyle: const TextStyle(color: Colors.black),
                    selectionOverlay: Container(),
                    onSelect: (Picker picker, int i, List<int> value) {
                      _selectedDate = picker.adapter.getText().split(' ')[0];
                    },
                ).makePicker(ThemeData())
              ),
            ],
          ),
        ),
      ),
    );
  }
}
